package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.service.AdChannelService;
import com.heima.common.exception.CustException;
import com.heima.model.admin.dtos.ChannelDTO;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;


@Service
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {
//    @Autowired
//    AdChannelMapper mapper;

    @Override
    public ResponseResult findByNameAndPage(ChannelDTO dto) {
//        参数检查
        if (dto == null){
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID);
//            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        dto.checkParam();
//        分页查询
        IPage<AdChannel> page = new Page<>(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<AdChannel> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(dto.getName())){
            wrapper.likeRight(AdChannel::getName,dto.getName());
        }
//        默认排序
        wrapper.orderByAsc(AdChannel::getOrd);
        IPage<AdChannel> channelIPage = this.page(page, wrapper);
        return new PageResponseResult(dto.getPage(),dto.getSize(),channelIPage.getTotal(),channelIPage.getRecords());
    }

    @Override
    public ResponseResult saveChannel(AdChannel channel) {
//        参数检查
        if (channel==null || StringUtils.isEmpty(channel.getName())){
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID);
//            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        if (channel.getName().length() > 10){
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID,"名称超过10个字符");
        }
//        检查频道是否已存在
        int count = this.count(new LambdaQueryWrapper<AdChannel>().eq(AdChannel::getName, channel.getName()));
        if (count > 0){
            CustException.cust(AppHttpCodeEnum.DATA_EXIST,"频道已存在");
//            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"频道已存在");
        }
        channel.setCreatedTime(new Date());
        save(channel);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult updateChannel(AdChannel channel) {
//        参数检查
        if (channel==null || channel.getId()==null || StringUtils.isEmpty(channel.getName())){
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID);
//            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
//        检查ID
        int countID = this.count(new LambdaQueryWrapper<AdChannel>().eq(AdChannel::getId, channel.getId()));
        if (countID == 0){
            CustException.cust(AppHttpCodeEnum.ADMING_CHANNEL_NOT_EXIST,"频道不存在");
//            return ResponseResult.errorResult(AppHttpCodeEnum.ADMING_CHANNEL_NOT_EXIST,"频道不存在");
        }
        if (channel.getName().length() > 10){
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID,"名称超过10个字符");
        }
//        检查频道名是否重复
        AdChannel adChannel = getById(channel.getId());
        if(StringUtils.isNotBlank(channel.getName()) && !channel.getName().equals(adChannel.getName())){
            int countName = this.count(new LambdaQueryWrapper<AdChannel>().eq(AdChannel::getName, channel.getName()));
            if (countName > 0){
                CustException.cust(AppHttpCodeEnum.DATA_EXIST,"同名频道已存在");
//                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"同名频道已存在");
            }
        }
        updateById(channel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult deleteChannel(Integer id) {
//        参数检查
        if (id == null){
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID);
//            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
//        检查频道是否存在
        AdChannel channel = this.getById(id);
        if (channel == null){
            CustException.cust(AppHttpCodeEnum.ADMING_CHANNEL_NOT_EXIST,"频道不存在");
//            return ResponseResult.errorResult(AppHttpCodeEnum.ADMING_CHANNEL_NOT_EXIST,"频道不存在");
        }
//        检查频道状态是否有效
        if (channel.getStatus()){
            CustException.cust(AppHttpCodeEnum.ADMING_CHANNEL_STATUS_TRUE,"频道启用状态不能删除");
//            return ResponseResult.errorResult(AppHttpCodeEnum.ADMING_CHANNEL_STATUS_TRUE);
        }
        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

}
